import {createSlice} from '@reduxjs/toolkit'
import React from 'react'
import storage from '../../utils/storeUtils'
import {TOKENKEY,USERKEY} from '../../config/constant'
const adminSlice=createSlice({
    name:'adminNS',
    initialState:{
        token:storage.getStorage(TOKENKEY)||'',
        user:storage.getStorage(USERKEY)||{},
    },
    reducers:{
       SET_TOKEN(state,action){
         state.token=action.payload
         storage.setStorage(TOKENKEY,action.payload)
       },
       SET_USER(state,action){
        state.user=action.payload
        storage.setStorage(USERKEY,action.payload)
       }
    }
})
export const loginAsync=payload=>{
    return async(dispatch)=>{
        const result=await React.$api.admin.loginApi(payload)
        dispatch(SET_TOKEN(result.data.token))
        dispatch(SET_USER(result.data.user))
        return result
    }
}
export const {SET_TOKEN,SET_USER}=adminSlice.actions
export default adminSlice.reducer